package com.dries.portal.module.coupon.enums;

import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Objects;
import java.util.stream.Stream;

/**
 * @Description: 优惠券有效期类型
 * @Author ZSY
 * @createTime 2020/7/27 10:35
 */
@Getter
@AllArgsConstructor
@NoArgsConstructor
public enum PeriodType {

    REGULAR(1, "固定日期"),
    SHIFT(2, "动态日期（以领取之日开始计算）");

    private Integer code;
    private String desc;

    @JsonValue
    public String getDesc() {
        return desc;
    }

    public static PeriodType of(Integer code) {
        Objects.requireNonNull(code);

        return Stream.of(values()).filter(e -> e.getCode().equals(code))
                .findAny()
                .orElseThrow(() -> new IllegalArgumentException(code + "not exists!"));
    }

}
